import 'package:flutter/material.dart';
import 'package:tijarat/utils/page_transition.dart';
import 'package:tijarat/screens/home_screen.dart';
import 'package:tijarat/screens/inventory_screen.dart';
import 'package:tijarat/screens/sales_screen.dart';
import 'package:tijarat/screens/customers_screen.dart';
import 'package:tijarat/screens/reports_screen.dart';
import 'package:tijarat/screens/settings_screen.dart';
import 'package:tijarat/screens/about_screen.dart';
import 'package:tijarat/screens/others_screen.dart';
import 'package:tijarat/screens/help_screen.dart';

class MainScreen extends StatefulWidget {
  const MainScreen({Key? key}) : super(key: key);

  @override
  State<MainScreen> createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
  Widget _currentScreen = const HomeScreen();
  String _currentTitle = 'باش بەت';

  void _navigateToScreen(Widget screen, String title) {
    setState(() {
      _currentScreen = AnimatedSwitcher(
        duration: const Duration(milliseconds: 300),
        transitionBuilder: (Widget child, Animation<double> animation) {
          return FadeTransition(
            opacity: animation,
            child: ScaleTransition(
              scale: animation,
              child: child,
            ),
          );
        },
        child: screen,
      );
      _currentTitle = title;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Directionality(
      textDirection: TextDirection.rtl,
      child: Scaffold(
        body: Row(
          children: [
            // يان تىزىملىك
            _buildSidebar(),

            // ئاساسىي مەزمۇن
            Expanded(
              child: _currentScreen,
            ),
          ],
        ),
      ),
    );
  }

  // يان تىزىملىك
  Widget _buildSidebar() {
    return Container(
      width: 280,
      color: Colors.white,
      child: Column(
        children: [
          // لوگو قىسمى
          _buildLogo(),

          // تىزىملىك تۈرلىرى
          Expanded(
            child: ListView(
              padding: const EdgeInsets.all(16),
              children: [
                _buildMenuItem(
                  icon: Icons.home,
                  title: 'باش بەت',
                  isSelected: _currentTitle == 'باش بەت',
                  onTap: () => _navigateToScreen(
                    const HomeScreen(),
                    'باش بەت',
                  ),
                ),
                _buildMenuItem(
                  icon: Icons.inventory,
                  title: 'مال كىرگۈزۈش',
                  isSelected: _currentTitle == 'مال كىرگۈزۈش',
                  onTap: () => _navigateToScreen(
                    const InventoryScreen(),
                    'مال كىرگۈزۈش',
                  ),
                ),
                _buildMenuItem(
                  icon: Icons.shopping_cart,
                  title: 'مال سېتىش',
                  isSelected: _currentTitle == 'مال سېتىش',
                  onTap: () => _navigateToScreen(
                    const SalesScreen(),
                    'مال سېتىش',
                  ),
                ),
                _buildMenuItem(
                  icon: Icons.warehouse,
                  title: 'ئامبار باشقۇرۇش',
                  isSelected: _currentTitle == 'ئامبار باشقۇرۇش',
                  onTap: () => _navigateToScreen(
                    const InventoryScreen(),
                    'ئامبار باشقۇرۇش',
                  ),
                ),
                _buildMenuItem(
                  icon: Icons.account_balance_wallet,
                  title: 'مالىيە باشقۇرۇش',
                  isSelected: _currentTitle == 'مالىيە باشقۇرۇش',
                  onTap: () => _navigateToScreen(
                    const SalesScreen(),
                    'مالىيە باشقۇرۇش',
                  ),
                ),
                _buildMenuItem(
                  icon: Icons.bar_chart,
                  title: 'دوكلاتلار',
                  isSelected: _currentTitle == 'دوكلاتلار',
                  onTap: () => _navigateToScreen(
                    const ReportsScreen(),
                    'دوكلاتلار',
                  ),
                ),
                _buildMenuItem(
                  icon: Icons.settings,
                  title: 'تەڭشەكلەر',
                  isSelected: _currentTitle == 'تەڭشەكلەر',
                  onTap: () => _navigateToScreen(
                    const SettingsScreen(),
                    'تەڭشەكلەر',
                  ),
                ),
                _buildMenuItem(
                  icon: Icons.help,
                  title: 'ياردەم ۋە باشقىلار',
                  isSelected: _currentTitle == 'ياردەم ۋە باشقىلار',
                  onTap: () => _navigateToScreen(
                    const OthersScreen(),
                    'ياردەم ۋە باشقىلار',
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  // لوگو قىسمى
  Widget _buildLogo() {
    return Container(
      padding: const EdgeInsets.all(16),
      child: Row(
        children: [
          Container(
            width: 40,
            height: 40,
            decoration: BoxDecoration(
              color: Colors.blue,
              borderRadius: BorderRadius.circular(8),
            ),
            child: const Icon(
              Icons.shopping_bag,
              color: Colors.white,
            ),
          ),
          const SizedBox(width: 16),
          const Text(
            'تىجارەت',
            style: TextStyle(
              fontSize: 24,
              fontWeight: FontWeight.bold,
            ),
          ),
        ],
      ),
    );
  }

  // تىزىملىك تۈرى
  Widget _buildMenuItem({
    required IconData icon,
    required String title,
    required bool isSelected,
    required VoidCallback onTap,
  }) {
    return Material(
      color: Colors.transparent,
      child: InkWell(
        onTap: onTap,
        borderRadius: BorderRadius.circular(8),
        child: AnimatedContainer(
          duration: const Duration(milliseconds: 200),
          padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
          decoration: BoxDecoration(
            color:
                isSelected ? Colors.blue.withOpacity(0.1) : Colors.transparent,
            borderRadius: BorderRadius.circular(8),
          ),
          child: Row(
            children: [
              Icon(
                icon,
                color: isSelected ? Colors.blue : Colors.grey[600],
              ),
              const SizedBox(width: 16),
              Text(
                title,
                style: TextStyle(
                  color: isSelected ? Colors.blue : Colors.grey[800],
                  fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
